package com.share.device.feign;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.share.common.core.constant.DeviceConstants;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.security.annotation.RequiresLogin;
import com.share.device.service.IDeviceService;
import com.share.device.service.IRegionService;
import com.share.device.service.IStationService;
import com.share.system.api.domain.Region;
import com.share.system.api.domain.Station;
import com.share.system.api.service.StationRedisService;
import com.share.system.api.vo.StationVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.share.common.core.web.domain.AjaxResult.success;

@RestController
@Slf4j
@RequestMapping("/api/device")
@Tag(name = "设备信息openfenig接口管理")
public class DeviceClient {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IStationService stationService;

    @Autowired
    private StationRedisService stationRedisService;

    @Autowired
    private IDeviceService deviceService;

    /**
     * 获取地区表数据数量
     * @return
     */
    @PostMapping("/getDeviceCount")
    public int getDeviceCount() {
        return regionService.getTableCount();
    }

    /**
     * 根据条件获取地区列表
     * @param page
     * @return
     */
    @GetMapping("/getDeviceRegionListByPageAndWapper")
    public List<Region> getDeviceRegionListByPageAndWapper(@RequestParam("page") int page, @RequestParam("size") int pageSize) {
        return regionService.getAllRegion(page,pageSize);
    }


    @PostMapping("/getStationCount")
    int getStationCount(){
        return stationService.getTableCount();
    }

    @GetMapping("/getStationCountListByPageAndWapper")
    List<Station> getStationCountListByPageAndWapper(
            @RequestParam("page") int page,
            @RequestParam("size") int size){
        return stationService.getAllStation(page,size);
    }


    @Operation(summary = "根据经纬度搜索附近门店（redis中获取）")
    @RequiresLogin
    @GetMapping("/nearbyStation/{latitude}/{longitude}")
    public AjaxResult nearbyStation(@PathVariable String latitude, @PathVariable String longitude)
    {
        List<StationVo> stationVoList = deviceService.nearbyRedisStation(latitude, longitude, DeviceConstants.SEARCH_H5_RADIUS);
        if (CollectionUtils.isEmpty(stationVoList)) {
            return AjaxResult.warn("周围暂无门店");
        }
        return success(stationVoList);
    }

    @Operation(summary = "根据id获取门店详情")
    @RequiresLogin
    @GetMapping("/getStation/{id}/{latitude}/{longitude}")
    public AjaxResult getStation(@PathVariable Long id, @PathVariable String latitude, @PathVariable String longitude)
    {
        return success(deviceService.getStation(id, latitude, longitude));
    }

    @Operation(summary = "扫码充电")
    @RequiresLogin
    @GetMapping("scanCharge/{cabinetNo}")
    public AjaxResult scanCharge(@PathVariable String cabinetNo) {
        return success(deviceService.scanCharge(cabinetNo));
    }

}
